var Observer = require("Observer");
var NetSocket = require('NetSocket');
var GameLocalStorage = require("GameLocalStorage");
var Utils = require("Utils");
var PrefabLoader = require('PrefabLoader');
var GameData = require('GameData');
cc.Class({
    extends: Observer,

    properties: {
        loginBtn: {default: null, displayName: "登录按钮", type: cc.Node},
        addNode: {default: null, displayName: "添加节点", type: cc.Node},
    },
    _getMsgList(){
        return [
            MJNetMsg.recv.Login.msg,// 登录返回
            GameGlobalMsg.Socket.Close,
            GameGlobalMsg.Socket.Open,
            GameGlobalMsg.Socket.Send,
            GameGlobalMsg.Socket.Recv,
            GameGlobalMsg.Socket.Error,
        ];
    },
    _onMsg(msg, data){
        if (msg == MJNetMsg.recv.Login.msg) {
            cc.director.loadScene("Center");
        } else if (msg == GameGlobalMsg.Socket.Open) {
            this._sendLoginNetMsg();
        } else if (msg == GameGlobalMsg.Socket.Error) {
            PrefabLoader.addTipsDialog("断开服务器链接\n请重试...",
                this._onReconnectNet.bind(this),
                this._onShowLoginBtn.bind(this)
            );
        }
    },
    _sendLoginNetMsg(){
        var sendData = {
            type: "ANONYMOUS",
            openId: null,
            code: null,
            longitude: null,
            latitude: null,
        };

        NetSocket.send(MJNetMsg.send.Login, sendData);
        PrefabLoader.addLoadingLayer(this.addNode);
    },
    _onShowLoginBtn(){
        this.loginBtn.active = true;
    },
    _onReconnectNet(){
        this.loginBtn.active = false;
        NetSocket.init();
    },
    onLoad: function () {
        GameData.initEvent();
        this._initMsg();
        GameLocalStorage.initStorage();
    },

    _onLoginError(){
        this.addNode.destroyAllChildren();
    },
    onLogin(){
        //cc.director.loadScene("Center");
        //return;
        this.loginBtn.active = false;
        NetSocket.init();
        //GameLocalStorage.setUserAndPwd(userName, pwd);
        //GameLocalStorage.save();
    }
});
